# -*- coding: utf-8 -*-
"""
Created on Sun Mar 21 16:27:34 2021

GRAPE

@author: Waikikilick
"""
import numpy as np
import matplotlib.pyplot as plt
import time
from qutip import *
from scipy.linalg import expm
import qutip.control.pulseoptim as cpo
import random
np.random.seed(1)
random.seed (1)

alg = 'GRAPE' 
# alg = 'CRAB'
#Algorithm to use in pulse optimisation. Options are:
#'GRAPE' (default) - GRadient Ascent Pulse Engineering 
#'CRAB' - Chopped RAndom Basis

target_state = np.mat([[1],[0]],dtype=complex)
# target_state = np.mat([[0],[1]],dtype=complex)

num_rep = 1 #times of repetition. the average fidelity are over it.


init_states = [ np.mat([[0.43388374+0.j], [0.86094135+0.26556524j]]), 
                np.mat([[0.6234898+0.j],  [0.7470969+0.23044887j]]), 
                np.mat([[0.78183148+0.j], [0.6234898 +0.j]]), 
                np.mat([[0.22252093+0.j], [-0.21694187-0.95048443j]]), 
                np.mat([[ 0.6234898 +0.j],[-0.77309907+0.11652594j]]), 
                np.mat([[0.90096887+0.j], [0.35849157+0.24441541j]]), 
                np.mat([[ 0.22252093+0.j],[-0.21694187+0.95048443j]]), 
                np.mat([[ 0.78183148+0.j],[-0.61652594-0.09292633j]]), 
                np.mat([[0.43388374+0.j], [0.74441541-0.50753383j]]), 
                np.mat([[0.90096887+0.j], [0.03242417-0.43267051j]]), 
                np.mat([[0.78183148+0.j], [0.04659345-0.6217464j]]), 
                np.mat([[0.6234898 +0.j], [0.64597949-0.44042136j]]), 
                np.mat([[ 0.6234898 +0.j],[-0.17397387-0.76222933j]]), 
                np.mat([[0.78183148+0.j], [0.51515145+0.35122431j]]), 
                np.mat([[0.43388374+0.j], [0.86094135-0.26556524j]]), 
                np.mat([[ 0.6234898 +0.j],[-0.39091574+0.67708593j]]), 
                np.mat([[ 0.22252093+0.j],[-0.48746396-0.84431234j]]), 
                np.mat([[ 0.78183148+0.j],[-0.3117449 -0.53995801j]]), 
                np.mat([[ 0.90096887+0.j],[-0.21694187+0.37575434j]]), 
                np.mat([[0.6234898 +0.j], [0.28563511+0.7277864j]]), 
                np.mat([[ 0.43388374+0.j],[-0.45048443+0.78026193j]]), 
                np.mat([[ 0.78183148+0.j],[-0.5617449 -0.27052209j]]), 
                np.mat([[ 0.90096887+0.j],[-0.09654821+0.42300537j]]), 
                np.mat([[ 0.22252093+0.j],[-0.8783797 +0.42300537j]]), 
                np.mat([[0.97492791+0.j], [0.21263495+0.0655892j]]), 
                np.mat([[ 0.6234898 +0.j],[-0.17397387+0.76222933j]]), 
                np.mat([[ 0.22252093+0.j],[-0.96403877+0.14530547j]]), 
                np.mat([[ 0.43388374+0.j],[-0.8117449 +0.39091574j]]), 
                np.mat([[0.97492791+0.j], [0.18385542+0.12535051j]]), 
                np.mat([[0.22252093+0.j], [0.35618116+0.9075348j]]), 
                np.mat([[0.90096887+0.j], [0.35849157-0.24441541j]]), 
                np.mat([[ 0.97492791+0.j],[-0.16311939-0.15135267j]]), 
                np.mat([[ 0.6234898 +0.j],[-0.70440583-0.33922397j]]), 
                np.mat([[ 0.90096887+0.j],[-0.31805929+0.29511589j]]), 
                np.mat([[ 0.43388374+0.j],[-0.66045692+0.61281446j]]), 
                np.mat([[0.+0.j],  [1.+0.j]]), 
                np.mat([[ 0.22252093+0.j],[-0.8783797 -0.42300537j]]), 
                np.mat([[0.43388374+0.j], [0.06732949+0.89844958j]]), 
                np.mat([[0.97492791+0.j], [0.22252093+0.j]]), 
                np.mat([[ 0.43388374+0.j],[-0.45048443-0.78026193j]]), 
                np.mat([[ 0.22252093+0.j],[-0.71467273-0.66311939j]]), 
                np.mat([[ 0.6234898 +0.j],[-0.39091574-0.67708593j]]), 
                np.mat([[ 0.78183148+0.j],[-0.45705037-0.42408077j]]), 
                np.mat([[ 0.78183148+0.j],[-0.3117449 +0.53995801j]]), 
                np.mat([[0.22252093+0.j], [0.9316146 +0.28736505j]]), 
                np.mat([[0.97492791+0.j], [0.13873953+0.17397387j]]), 
                np.mat([[0.78183148+0.j], [0.5957899 -0.18377685j]]), 
                np.mat([[ 0.97492791+0.j],[-0.20048443-0.09654821j]]), 
                np.mat([[0.90096887+0.j], [0.03242417+0.43267051j]]), 
                np.mat([[0.43388374+0.j], [0.06732949-0.89844958j]]), 
                np.mat([[ 0.78183148+0.j],[-0.13873953+0.60785761j]]), 
                np.mat([[0.6234898+0.j],  [0.7470969-0.23044887j]]), 
                np.mat([[0.78183148+0.j], [0.38873953-0.48746396j]]), 
                np.mat([[ 0.78183148+0.j],[-0.13873953-0.60785761j]]), 
                np.mat([[0.22252093+0.j], [0.60785761-0.76222933j]]), 
                np.mat([[0.6234898 +0.j], [0.78183148+0.j]]), 
                np.mat([[ 0.97492791+0.j],[-0.04951557-0.21694187j]]), 
                np.mat([[ 0.90096887+0.j],[-0.42903762-0.06466702j]]), 
                np.mat([[ 0.97492791+0.j],[-0.16311939+0.15135267j]]), 
                np.mat([[ 0.43388374+0.j],[-0.66045692-0.61281446j]]), 
                np.mat([[0.43388374+0.j], [0.5617449 -0.70440583j]]), 
                np.mat([[0.22252093+0.j], [0.60785761+0.76222933j]]), 
                np.mat([[ 0.6234898 +0.j],[-0.70440583+0.33922397j]]), 
                np.mat([[ 0.90096887+0.j],[-0.31805929-0.29511589j]])]

s_x = np.mat([[0, 1], [1, 0]], dtype=complex)
s_z = np.mat([[1, 0], [0, -1]],dtype=complex)

Drift_Hamiltonian = Qobj(s_x)
Control_hamiltonian = [Qobj(s_z)]

num_timesteps = 20 #number of timeslots.
evolution_time = 2 * np.pi #total time for the evolution
dt = np.pi/10 #pulse duration
amp_lbound = 0 #lower boundaries for the control amplitudes
amp_ubound = 4 #upper boundaries for the control amplitudes

fidelity_error_required = 1e-4 #Fidelity error target. Pulse optimisation will terminate when the fidelity error falls below this value
max_iter = 5000 #Maximum number of iterations of the optimisation algorithm
max_wall_time = 12 #Maximum allowed elapsed time for the optimisation algorithm

minimum_gradient = 0.01 # When the sum of the squares of the gradients wrt to the control amplitudes falls below this value, the optimisation terminates, assuming local minima
p_type = 'RND' #type / shape of pulse(s) used to initialise the the control amplitudes. Options (GRAPE) include:
# RND, LIN, ZERO, SINE, SQUARE, TRIANGLE, SAW DEF is RND


data = []
for init_state in init_states:
    
    
    start_time = time.time()
    
        
    #process optimization
    result = cpo.optimize_pulse_unitary(Drift_Hamiltonian, Control_hamiltonian, Qobj(init_state), Qobj(target_state), 
                                        num_timesteps, evolution_time, amp_lbound=amp_lbound,amp_ubound=amp_ubound,
                                        alg=alg,fid_err_targ=fidelity_error_required, min_grad=minimum_gradient, 
                                        max_iter=max_iter, max_wall_time=max_wall_time, init_pulse_type=p_type, 
                                        gen_stats=True )

    # Output results
    # result.stats.report()
    # print("Final evolution\n{}\n".format(result.evo_full_final))
    # print(result.termination_reason)

    # plot = plt.figure()
    # axis1 = plot.add_subplot(2, 1, 1)
    # axis1.set_title("Initial control amplitudes")
    # axis1.set_ylabel("Control amplitude")
    # axis1.step(result.time,
    #         np.hstack((result.initial_amps[:, 0], result.initial_amps[-1, 0])),
    #         where='post')

    # axis2 = plot.add_subplot(2, 1, 2)
    # axis2.set_title("Optimised Control Sequences")
    # axis2.set_xlabel("Time")
    # axis2.set_ylabel("Control amplitude")
    # axis2.step(result.time,
    #         np.hstack((result.final_amps[:, 0], result.final_amps[-1, 0])),
    #         where='post')
    # plt.tight_layout()
    # plt.show()
    
    # 不对脉冲进行整数化操作 
    # raw_fid_list = []
    # psi = init_state
    
    # for action in result.final_amps:
    #     H =  float(action)* s_z + 1 * s_x
    #     U = expm(-1j * H * dt)
    #     psi = U * psi  # next state
        
    # fid = (np.abs(psi.H * target_state) ** 2).item(0).real  
    # raw_fid_list.append(fid)    
    
    seq_int = np.rint(result.final_amps)
    # seq_int = result.final_amps
    psi = init_state
    fid_list = [(np.abs(psi.H * target_state) ** 2).item(0).real ]
    for action in seq_int:
        H =  float(action)* s_z + 1 * s_x
        U = expm(-1j * H * dt)
        psi = U * psi  # next state
        fid = (np.abs(psi.H * target_state) ** 2).item(0).real 
        fid_list.append(fid)       
    end_time = time.time()
    fid_max = max(fid_list)
    max_index = fid_list.index(fid_max)
    data.append(((end_time-start_time), fid_max, max_index))
    
# print('mean of raw fid list = ', np.mean(raw_fid_list))
  
data.sort()

time = 0
fid = 0
max_index = 0
time_list = []
fid_list = []
max_index_list = []

for i in data:
    time += i[0]
    time_list.append(i[0])
    fid += i[1]
    fid_list.append(i[1])
    max_index += i[2]
    
print('the mean time is: ', time/len(init_states))
print('the mean fid is: ', fid/len(init_states))
print('the mean max_index is: ', max_index/len(init_states))

for i in (data):
    print(i)  
    
# print(np.array(time_list))
# print(np.array(fid_list))
  
# GRAPE to |0>

    # the mean time is:  0.02681252732872963
    # 0.02094412 0.02194071 0.02194118 0.02194142 0.02194142 0.02194142 0.02194142 0.0219593  0.02293873 0.02293897 0.02293921 0.02294064 0.02393484 0.02393556 0.02393579 0.02393603 0.02393603 0.02393627 0.0239439  0.02491593 0.02493072 0.02493143 0.02493167 0.02493238 0.02493286 0.02493286 0.02493286 0.0249331  0.0249331  0.02493358 0.02493358 0.02493382 0.02493572 0.02493954 0.02592492 0.0259304 0.02593064 0.02593088 0.02692723 0.02692747 0.02692842 0.02792501 0.02792525 0.02792597 0.02792597 0.02792692 0.02892041 0.02892256 0.02991748 0.02991843 0.02991915 0.02991962 0.02992153 0.02993798 0.03091621 0.03091741 0.03091908 0.03091955 0.03291059 0.03489923 0.03490591 0.03889799 0.03989196 0.04146743
    # the mean fid is:  0.9721103587394687
    # 0.99965717 0.97987119 0.96337676 0.87443563 0.93639066 0.93771081 0.98854877 0.99909738 0.99151826 0.98950125 0.87827101 0.99896284 0.99136236 0.98727251 0.99146634 0.99460728 0.99524556 0.99431713 0.98149863 0.98636358 0.99851841 0.97167013 0.99790245 0.92757493 0.98029355 0.99311413 0.99852838 0.96729535 0.99684059 0.98792247 0.98868047 0.97976663 0.99156124 0.95611934 0.98809215 0.98945005 0.95048443 0.99986513 0.96820023 0.94397416 0.96355468 0.97967695 0.89687575 0.96835467 0.98691432 0.92427833 0.99834541 0.98502244 0.98216165 0.98175365 0.96960125 0.9692314  0.9884277  0.99358046 0.93857866 0.99548317 0.98628493 0.97712813 0.90017485 0.96385818 0.91842835 0.98096208 0.96092889 0.97012772

    # (0.02094411849975586, 0.999657167583475)
    # (0.02194070816040039, 0.9798711859411483)
    # (0.021941184997558594, 0.9633767598391794)
    # (0.021941423416137695, 0.87443562893114)
    # (0.021941423416137695, 0.9363906603025534)
    # (0.021941423416137695, 0.9377108121084996)
    # (0.021941423416137695, 0.9885487722659139)
    # (0.021959304809570312, 0.9990973768847475)
    # (0.02293872833251953, 0.9915182584146968)
    # (0.022938966751098633, 0.9895012514321895)
    # (0.022939205169677734, 0.878271014704563)
    # (0.022940635681152344, 0.998962835594469)
    # (0.02393484115600586, 0.9913623550870437)
    # (0.023935556411743164, 0.9872725117951755)
    # (0.023935794830322266, 0.9914663368233286)
    # (0.023936033248901367, 0.9946072830019552)
    # (0.023936033248901367, 0.995245561804983)
    # (0.02393627166748047, 0.994317132454312)
    # (0.02394390106201172, 0.9814986260368019)
    # (0.02491593360900879, 0.9863635772067273)
    # (0.024930715560913086, 0.9985184118528798)
    # (0.02493143081665039, 0.9716701267547768)
    # (0.024931669235229492, 0.9979024546046494)
    # (0.024932384490966797, 0.9275749290949737)
    # (0.024932861328125, 0.980293552540621)
    # (0.024932861328125, 0.9931141345205131)
    # (0.024932861328125, 0.9985283792875503)
    # (0.0249330997467041, 0.9672953484333287)
    # (0.0249330997467041, 0.9968405856287794)
    # (0.024933576583862305, 0.9879224670325734)
    # (0.024933576583862305, 0.9886804698200811)
    # (0.024933815002441406, 0.9797666260296951)
    # (0.02493572235107422, 0.9915612363474885)
    # (0.024939537048339844, 0.9561193371187858)
    # (0.0259249210357666, 0.9880921543787979)
    # (0.025930404663085938, 0.9894500546279927)
    # (0.02593064308166504, 0.9504844296969681)
    # (0.02593088150024414, 0.9998651322225649)
    # (0.02692723274230957, 0.9682002288102741)
    # (0.026927471160888672, 0.9439741634061761)
    # (0.026928424835205078, 0.9635546789262042)
    # (0.02792501449584961, 0.9796769490319778)
    # (0.02792525291442871, 0.8968757530816815)
    # (0.027925968170166016, 0.9683546704266518)
    # (0.027925968170166016, 0.9869143160904318)
    # (0.027926921844482422, 0.9242783332790545)
    # (0.028920412063598633, 0.9983454136109028)
    # (0.028922557830810547, 0.9850224372548357)
    # (0.029917478561401367, 0.982161646158848)
    # (0.029918432235717773, 0.9817536450616275)
    # (0.029919147491455078, 0.9696012521684343)
    # (0.02991962432861328, 0.9692313957778331)
    # (0.029921531677246094, 0.9884276958772802)
    # (0.0299379825592041, 0.9935804600989125)
    # (0.030916213989257812, 0.9385786649728262)
    # (0.03091740608215332, 0.9954831735279577)
    # (0.03091907501220703, 0.9862849345158503)
    # (0.030919551849365234, 0.9771281254678552)
    # (0.03291058540344238, 0.9001748532837937)
    # (0.034899234771728516, 0.9638581798903976)
    # (0.03490591049194336, 0.9184283544545236)
    # (0.03889799118041992, 0.9809620793658406)
    # (0.039891958236694336, 0.9609288947788589)
    # (0.04146742820739746, 0.9701277218010463)


# CRAB to |0>   

    # the mean time is:  0.7503959238529205
    # 0.07582164 0.11868334 0.20744371 0.2231741  0.25733709 0.26337576 0.2687099  0.26992607 0.27644753 0.285218   0.32211685 0.32223129 0.32996345 0.33608222 0.35408115 0.36713529 0.38999772 0.39042306 0.39196992 0.39990616 0.4079628  0.40830398 0.41788268 0.4212625 0.44187713 0.44278407 0.45899272 0.46800351 0.48344755 0.48871803 0.51559663 0.52855825 0.52875352 0.52959394 0.53360724 0.54061723 0.55854297 0.56150484 0.58042955 0.59357285 0.60533452 0.64228201 0.70810914 0.74298191 0.74310708 0.74799252 0.7805419  0.79884219 0.84676147 0.86904454 1.0500412  1.11099792 1.12051439 1.16887188 1.30265665 1.37390637 1.40726376 1.41344309 1.81018162 2.27338171 2.47751307 2.61998725 2.81503487 2.83649182
    # the mean fid is:  0.9654843913909724
    # 0.97762621 0.99079556 0.98188594 0.99086178 0.922488   0.94957244 0.98348179 0.99912969 0.9856178  0.9762125  0.79679226 0.95048443 0.89009931 0.85807448 0.98448161 0.94746686 0.9766167  0.98718739 0.98259513 0.9834018  0.95048443 0.99101164 0.99439839 0.99504585 0.99196083 0.97520659 0.98448997 0.98875271 0.99201431 0.9893915 0.97491459 0.95250047 0.99179841 0.9908832  0.98740326 0.96879129 0.98578477 0.84447625 0.97622632 0.9957886  0.97762257 0.99563563 0.90184401 0.98034713 0.89104441 0.99291147 0.95051882 0.94596037 0.95242023 0.99150756 0.98133894 0.98925246 0.92719047 0.98156767 0.97654305 0.98462656 0.98164076 0.95959517 0.96175885 0.98651041 0.98351534 0.98083651 0.96554724 0.9150704 

    # (0.0758216381072998, 0.9776262059258969)
    # (0.1186833381652832, 0.9907955554209203)
    # (0.2074437141418457, 0.9818859367312867)
    # (0.2231740951538086, 0.9908617847485728)
    # (0.2573370933532715, 0.9224880026973495)
    # (0.26337575912475586, 0.9495724385908451)
    # (0.2687098979949951, 0.983481792349529)
    # (0.2699260711669922, 0.9991296887680837)
    # (0.2764475345611572, 0.9856178030924236)
    # (0.2852180004119873, 0.9762124951717377)
    # (0.3221168518066406, 0.7967922606595524)
    # (0.3222312927246094, 0.9504844296969681)
    # (0.32996344566345215, 0.8900993086649537)
    # (0.33608222007751465, 0.858074478141254)
    # (0.3540811538696289, 0.9844816108573248)
    # (0.36713528633117676, 0.9474668573188015)
    # (0.3899977207183838, 0.9766167004673652)
    # (0.390423059463501, 0.9871873907283628)
    # (0.3919699192047119, 0.9825951309806287)
    # (0.3999061584472656, 0.9834017986939076)
    # (0.4079627990722656, 0.9504844296969681)
    # (0.40830397605895996, 0.991011640208211)
    # (0.41788268089294434, 0.994398394452857)
    # (0.4212625026702881, 0.9950458499201094)
    # (0.4418771266937256, 0.991960831184956)
    # (0.44278407096862793, 0.9752065875664788)
    # (0.45899271965026855, 0.9844899698174656)
    # (0.468003511428833, 0.9887527078467397)
    # (0.4834475517272949, 0.9920143125341415)
    # (0.48871803283691406, 0.9893914967093976)
    # (0.5155966281890869, 0.9749145857841545)
    # (0.5285582542419434, 0.9525004731429958)
    # (0.5287535190582275, 0.9917984094916764)
    # (0.5295939445495605, 0.990883196455131)
    # (0.5336072444915771, 0.9874032566574343)
    # (0.5406172275543213, 0.9687912870282717)
    # (0.5585429668426514, 0.9857847663708641)
    # (0.5615048408508301, 0.8444762499035761)
    # (0.5804295539855957, 0.9762263211405161)
    # (0.5935728549957275, 0.9957885952195048)
    # (0.6053345203399658, 0.97762256803411)
    # (0.6422820091247559, 0.9956356251749348)
    # (0.7081091403961182, 0.9018440116756279)
    # (0.7429819107055664, 0.9803471340485715)
    # (0.7431070804595947, 0.8910444062055499)
    # (0.7479925155639648, 0.9929114714610219)
    # (0.7805418968200684, 0.9505188193176767)
    # (0.798842191696167, 0.945960369083612)
    # (0.8467614650726318, 0.9524202284439567)
    # (0.8690445423126221, 0.9915075613867322)
    # (1.0500411987304688, 0.981338936822732)
    # (1.1109979152679443, 0.9892524585385823)
    # (1.1205143928527832, 0.927190473495468)
    # (1.1688718795776367, 0.9815676728449905)
    # (1.302656650543213, 0.9765430468710682)
    # (1.3739063739776611, 0.9846265551152583)
    # (1.4072637557983398, 0.9816407582441931)
    # (1.4134430885314941, 0.9595951689666893)
    # (1.8101816177368164, 0.9617588460057586)
    # (2.2733817100524902, 0.9865104109798826)
    # (2.477513074874878, 0.9835153416366118)
    # (2.6199872493743896, 0.9808365089955788)
    # (2.815034866333008, 0.965547242469263)
    # (2.836491823196411, 0.915070402367161)
 
  
# GRAPE to |1>

    # the mean time is:  0.02784058079123497
    # 0.02190852 0.02191639 0.02194214 0.02195406 0.02198577 0.02289319 0.02291083 0.02291274 0.02291989 0.02293873 0.02293873 0.02293944 0.02294397 0.02390623 0.0239346  0.0239346  0.02393532 0.02393603 0.02394772 0.02395439 0.02396083 0.02396107 0.0249033  0.02490425 0.02490902 0.02491999 0.02493215 0.02493334 0.02493334 0.02493382 0.02493405 0.02493501 0.02493787 0.02496362 0.02496505 0.02496648 0.02498817 0.02544355 0.02592039 0.02592897 0.02593136 0.02691603 0.02792907 0.02795029 0.02892447 0.02894688 0.02991915 0.0299201 0.02992082 0.02992249 0.02994418 0.03091526 0.03091788 0.03291106 0.03291345 0.03687096 0.03690076 0.03789926 0.03892493 0.03989339 0.04092145 0.04484844 0.05083299 0.05089498
    # the mean fid is:  0.9736632213971707
    # 0.99707153 0.97252742 0.98613264 0.95048443 0.9436207  0.98922569 0.97110752 0.99041468 0.99952037 0.99300828 0.99926051 0.97581023 0.97281885 0.98903216 0.93355396 0.97713759 0.99166423 0.99263121 0.84089824 0.98854807 0.99292434 0.98321409 0.9613336  0.89522003 0.97999382 0.99978219 0.96797654 0.90923946 0.98958059 0.96338778 0.98638836 0.99266063 0.98832237 0.97450868 0.99125482 0.99804861 0.97968318 0.9969386  0.99080743 0.88534779 0.9820373  0.99749209 0.96631616 0.99720007 0.94481096 0.97428922 0.95836564 0.93185187 0.99226043 0.98473222 0.98594326 0.99267292 0.9980834  0.99909306 0.99754599 0.90197246 0.98478906 1.         0.98336407 0.94715502 0.97383999 0.99972979 0.95048443 0.98933554
    
    # (0.02190852165222168, 0.997071525897701)
    # (0.02191638946533203, 0.9725274152116448)
    # (0.021942138671875, 0.9861326411938885)
    # (0.021954059600830078, 0.9504844348946624)
    # (0.021985769271850586, 0.9436207026386105)
    # (0.022893190383911133, 0.989225691219114)
    # (0.02291083335876465, 0.9711075232272289)
    # (0.02291274070739746, 0.9904146832829199)
    # (0.022919893264770508, 0.9995203679931637)
    # (0.02293872833251953, 0.993008276593909)
    # (0.02293872833251953, 0.9992605112698921)
    # (0.022939443588256836, 0.9758102283485467)
    # (0.022943973541259766, 0.9728188509282101)
    # (0.023906230926513672, 0.9890321604397366)
    # (0.023934602737426758, 0.9335539554879327)
    # (0.023934602737426758, 0.9771375941526202)
    # (0.023935317993164062, 0.9916642289639892)
    # (0.023936033248901367, 0.9926312085965764)
    # (0.023947715759277344, 0.8408982412531681)
    # (0.023954391479492188, 0.9885480682512823)
    # (0.02396082878112793, 0.9929243444057454)
    # (0.02396106719970703, 0.9832140872395349)
    # (0.024903297424316406, 0.9613335983763781)
    # (0.024904251098632812, 0.8952200286063953)
    # (0.024909019470214844, 0.9799938161872003)
    # (0.024919986724853516, 0.9997821876845445)
    # (0.024932146072387695, 0.9679765372299511)
    # (0.024933338165283203, 0.9092394635634137)
    # (0.024933338165283203, 0.9895805941696719)
    # (0.024933815002441406, 0.9633877818086336)
    # (0.024934053421020508, 0.9863883586133825)
    # (0.024935007095336914, 0.9926606326343005)
    # (0.024937868118286133, 0.9883223658666241)
    # (0.0249636173248291, 0.9745086834376717)
    # (0.02496504783630371, 0.9912548178194557)
    # (0.02496647834777832, 0.9980486110384545)
    # (0.024988174438476562, 0.9796831804045332)
    # (0.025443553924560547, 0.9969385997771738)
    # (0.025920391082763672, 0.9908074266482013)
    # (0.025928974151611328, 0.8853477878850318)
    # (0.025931358337402344, 0.982037299810949)
    # (0.026916027069091797, 0.9974920946003607)
    # (0.027929067611694336, 0.9663161636966755)
    # (0.027950286865234375, 0.9972000671722363)
    # (0.02892446517944336, 0.9448109634740788)
    # (0.028946876525878906, 0.9742892191569277)
    # (0.029919147491455078, 0.9583656402557856)
    # (0.029920101165771484, 0.9318518728082206)
    # (0.02992081642150879, 0.9922604277199515)
    # (0.0299224853515625, 0.9847322212079912)
    # (0.029944181442260742, 0.9859432596641552)
    # (0.030915260314941406, 0.9926729158982551)
    # (0.030917882919311523, 0.9980834017435495)
    # (0.032911062240600586, 0.9990930586336692)
    # (0.0329134464263916, 0.9975459854074443)
    # (0.03687095642089844, 0.9019724576391621)
    # (0.03690075874328613, 0.9847890642923804)
    # (0.03789925575256348, 1.0)
    # (0.0389249324798584, 0.983364072807406)
    # (0.039893388748168945, 0.9471550236874985)
    # (0.04092144966125488, 0.9738399894403064)
    # (0.04484844207763672, 0.9997297904715111)
    # (0.05083298683166504, 0.9504844266315219)
    # (0.050894975662231445, 0.9893355399577979)


# CRAB to |1>  

    # the mean time is:  0.6538934260606766
    # 0.12167454 0.12267184 0.14660692 0.1615684  0.22512484 0.2333765 0.25332236 0.27426696 0.27526402 0.2802453  0.3291204  0.34407949 0.34806967 0.3500843  0.35704446 0.37400794 0.38397312 0.39195156 0.40691113 0.40889883 0.42221403 0.42885327 0.42985082 0.43086886 0.44181871 0.44381285 0.4468081  0.44782615 0.45311213 0.45577955 0.46860266 0.47473097 0.48570156 0.50166512 0.5136261  0.52958322 0.54454231 0.54454374 0.5505271  0.56847954 0.57210898 0.58144522 0.58942342 0.59214377 0.59540629 0.59897161 0.61560988 0.62450647 0.62543583 0.62632656 0.64028788 0.71309304 0.75298715 0.8073504 0.81979871 0.81980705 1.002455   1.35358667 1.4311738  1.81716275 2.09647155 2.37764096 2.47741985 3.34735703
    # the mean fid is:  0.9680344309534588
    # 0.94625889 0.97964543 0.99728051 0.99204572 0.99321765 0.95094764 0.99780012 0.99372714 1.         0.98467159 0.8864022  0.92527859 0.99489433 0.95506777 0.97961819 0.996593   0.96793755 0.99120148 0.99070631 0.91721575 0.95202814 0.96927856 0.99956213 0.98262408 0.97052386 0.99078287 0.95899641 0.99679186 0.96883332 0.96836032 0.97187035 0.96686194 0.98445424 0.99372852 0.96547422 0.97279317 0.91645826 0.90872913 0.98912009 0.99302145 0.9645856  0.8096658 0.87493721 0.9832814  0.97957616 0.88269495 0.9651533  0.98189533 0.98285923 0.98683904 0.9966144  0.99522977 0.99745145 0.99554231 0.93748902 0.96793304 0.99416066 0.95534839 0.9952402  0.96823474 0.97569353 0.99151809 0.92520818 0.98624903

    # (0.1216745376586914, 0.9462588886969444)
    # (0.12267184257507324, 0.9796454296695412)
    # (0.1466069221496582, 0.9972805127718105)
    # (0.16156840324401855, 0.9920457188182812)
    # (0.22512483596801758, 0.9932176504196308)
    # (0.23337650299072266, 0.9509476363521919)
    # (0.2533223628997803, 0.9978001214538809)
    # (0.27426695823669434, 0.9937271416878987)
    # (0.27526402473449707, 1.0)
    # (0.280245304107666, 0.9846715887487594)
    # (0.329120397567749, 0.8864021951914653)
    # (0.34407949447631836, 0.925278588369011)
    # (0.3480696678161621, 0.9948943309922107)
    # (0.3500843048095703, 0.9550677703174497)
    # (0.3570444583892822, 0.9796181855740173)
    # (0.3740079402923584, 0.9965930037814786)
    # (0.3839731216430664, 0.967937554511557)
    # (0.3919515609741211, 0.9912014837877218)
    # (0.40691113471984863, 0.9907063092895443)
    # (0.40889883041381836, 0.9172157496141413)
    # (0.4222140312194824, 0.9520281394991511)
    # (0.42885327339172363, 0.9692785610444549)
    # (0.42985081672668457, 0.9995621282315312)
    # (0.43086886405944824, 0.9826240786631699)
    # (0.4418187141418457, 0.9705238623695894)
    # (0.44381284713745117, 0.9907828681085795)
    # (0.4468080997467041, 0.9589964057850868)
    # (0.4478261470794678, 0.9967918559655504)
    # (0.4531121253967285, 0.9688333179841281)
    # (0.4557795524597168, 0.9683603161179385)
    # (0.46860265731811523, 0.9718703453389623)
    # (0.4747309684753418, 0.9668619405912037)
    # (0.4857015609741211, 0.9844542426542819)
    # (0.5016651153564453, 0.9937285226108538)
    # (0.5136260986328125, 0.9654742204961332)
    # (0.529583215713501, 0.9727931732699545)
    # (0.5445423126220703, 0.9164582633611472)
    # (0.5445437431335449, 0.9087291334884526)
    # (0.5505270957946777, 0.989120093544123)
    # (0.5684795379638672, 0.9930214514036471)
    # (0.5721089839935303, 0.9645855974621796)
    # (0.5814452171325684, 0.8096658031168137)
    # (0.589423418045044, 0.8749372103762497)
    # (0.5921437740325928, 0.9832813982842965)
    # (0.5954062938690186, 0.9795761568224869)
    # (0.5989716053009033, 0.8826949460420445)
    # (0.615609884262085, 0.9651532971710067)
    # (0.6245064735412598, 0.98189532668728)
    # (0.6254358291625977, 0.9828592255646178)
    # (0.6263265609741211, 0.986839035135707)
    # (0.6402878761291504, 0.9966143989553932)
    # (0.7130930423736572, 0.9952297727033362)
    # (0.7529871463775635, 0.9974514518737867)
    # (0.8073503971099854, 0.9955423094177549)
    # (0.8197987079620361, 0.9374890183466498)
    # (0.8198070526123047, 0.9679330352547514)
    # (1.0024549961090088, 0.9941606599505082)
    # (1.3535866737365723, 0.9553483878838963)
    # (1.4311738014221191, 0.9952402039892531)
    # (1.8171627521514893, 0.9682347388094799)
    # (2.0964715480804443, 0.9756935308227528)
    # (2.377640962600708, 0.9915180855136907)
    # (2.477419853210449, 0.9252081841799604)
    # (3.3473570346832275, 0.986249026081977)